package org.cmroot.certmaster.model;

import com.google.gwt.logging.client.DefaultLevel;
import com.google.gwt.user.client.rpc.IsSerializable;
import org.cmroot.certmaster.shared.dto.DTO;
import org.cmroot.certmaster.shared.dto.Pkcs10DTO;
import org.cmroot.certmaster.shared.dto.ext.ExtDTO;
import org.cmroot.certmaster.shared.dto.ext.Pkcs10ExtDTO;
import org.hibernate.annotations.Cascade;

import javax.persistence.*;
import java.util.Set;

/**
 * Copyright (c) 2010 Andrey Domas <andrey.domas at gmail.com>.
 * *
 * This file is part of certmaster.
 * *
 * Certmaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * *
 * Certmaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 */

@Entity
@Table(name = "pkcs10")
public class PKCS10 extends PKCS {

    //signed certs
    @OneToMany
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    public Set<X509> getX509() { return this.x509; }
    public void setX509(Set<X509> certs) { this.x509 = certs; }
    private Set<X509> x509;

    @Column
    public CertStatus getStatus() { return this.status; }
    public void setStatus(CertStatus s) { this.status = s; }
    private CertStatus status = CertStatus.PENDING;

    public PKCS10() {}
    public PKCS10(byte[] der) { this.der = der; }

    @Override
    public Pkcs10DTO createDTO() {
        Pkcs10DTO dto = new Pkcs10DTO();
        dto.setId(getId());
        dto.setCN(getCN());
        dto.setChildCount(getX509() == null ? 0 : getX509().size() );
        return dto;
    }

    @Override
    public Pkcs10ExtDTO createExtDTO() {
        Pkcs10ExtDTO extDTO = new Pkcs10ExtDTO();

        extDTO.setId(getId());
        extDTO.setCN(getCN());
        extDTO.setChildCount(getX509() == null ? 0 : getX509().size() );

        return extDTO;
    }
}
